
#include "mapIntersection.hpp"
#include "../config/config.hpp"

MapIntersection::MapIntersection(Node *n)
{
	this->node = n;
	this->setId(this->node->getId());
	this->setMapElementType(INTERSECTION);
	this->setPositionX(this->node->getPositionX());
	this->setPositionY(this->node->getPositionY());
	this->setImageSource(Config::get("inteserction_image_source"));

	this->setImageWidth(10);
	this->setImageHeight(10);
	int i;
	for(i = 0; i < 4; i++ )
		this->bifurcationOptions[i] = 0;

}

MapIntersection::MapIntersection(std::string id)
{
	this->setId(id);
	this->setMapElementType(INTERSECTION);
	int i;
	for(i = 0; i < 4; i++ )
		this->bifurcationOptions[i] = 0;
}

bool MapIntersection::belongToMP()
{
	return this->node->getBelongToMP();
}

bool MapIntersection::isBifurcationOptionAvailable(BifurcationOption bo)
{
	return (this->bifurcationOptions[bo] == 1);
}

void MapIntersection::setBifurcationOptionAvailable(BifurcationOption bo)
{
	this->bifurcationOptions[bo] = 1;
}

void MapIntersection::setOutGoingSide(MapStreet *ms)
{
	int destinyPosX = ms->getIntersectionDestinyPositionX();
	int destinyPosY = ms->getIntersectionDestinyPositionY();

	//Eje vertical
	if(destinyPosX == this->getPositionX())
	{
		if(destinyPosY > this->getPositionY())
			//La calle va para abajo de la interseccion
			this->setBifurcationOptionAvailable(DOWN);
		else
			//La calle va para arriba de la interseccion
			this->setBifurcationOptionAvailable(UP);
	}
	//Eje horizontal
	else if(destinyPosY == this->getPositionY())
	{
		if(destinyPosX > this->getPositionX())
			//La calle va para la derecha de la interseccion
			this->setBifurcationOptionAvailable(RIGHT);
		else
			//La calle va para la izquierda de la interseccion
			this->setBifurcationOptionAvailable(LEFT);
	}
}

void MapIntersection::setInComingSide(MapStreet *ms)
{
	int originPosX = ms->getIntersectionOriginPositionX();
	int originPosY = ms->getIntersectionOriginPositionY();

	//Eje vertical
	if(originPosX == this->getPositionX())
	{
		if(originPosY > this->getPositionY())
			this->setBifurcationOptionAvailable(DOWN);
		else
			this->setBifurcationOptionAvailable(UP);
	}
	//Eje horizontal
	else if(originPosY == this->getPositionY())
	{
		if(originPosX > this->getPositionX())
			this->setBifurcationOptionAvailable(RIGHT);
		else
			this->setBifurcationOptionAvailable(LEFT);
	}
}